package com.xjcs.saplingplan.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.xjcs.saplingplan.R
import com.xjcs.saplingplan.const.loadImage
import com.xjcs.saplingplan.const.setSingleClick
import com.xjcs.saplingplan.const.toToast
import com.xjcs.saplingplan.databinding.ItemRemindBinding
import com.xjcs.saplingplan.dialog.AdvanceRemindDaysPop
import com.xjcs.saplingplan.model.bean.RemindBean
import com.xjcs.saplingplan.utils.DialogUtils

class RemindAdapter(
    private val context: Context,
    private val data: MutableList<RemindBean>,
    initSelectDaysStr: String = "",
    var initSelectTimeStr: String = "",
) : RecyclerView.Adapter<RemindAdapter.ItemRemindViewHolder>() {

    //用于记录当前选中的position(使用set避免重复)
    private val selectedPositions: MutableSet<Int> = mutableSetOf()

    //最多允许选中几个
    private companion object {
        const val MAX_SELECT_COUNT = 3
    }

    init {
        val selectSet = initSelectDaysStr
            .split(",")
            .mapNotNull { it.toIntOrNull() }
            .toMutableSet()
        selectSet.let { positions ->
            positions.take(MAX_SELECT_COUNT).forEach { pos ->
                selectedPositions.add(pos)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemRemindViewHolder {
        val binding = ItemRemindBinding.inflate(LayoutInflater.from(context), parent, false)
        return ItemRemindViewHolder(binding, selectedPositions) { pos -> onItemClick(pos) }
    }

    override fun getItemCount(): Int = data.size

    private fun onItemClick(pos: Int) {
        when (val dayNum = data[pos].dayNum) {
            999 -> {
                if (selectedPositions.size < MAX_SELECT_COUNT) {
                    //自定义提前
                    showAdvancePop()
                }else{
                    "最多只能选择3个提醒".toToast()
                }
            }
            -1 -> {
                //无提醒
                selectedPositions.clear()
                notifyDataSetChanged()
            }
            else -> {
                val isSelected = selectedPositions.contains(dayNum)
                if (isSelected) {
                    selectedPositions.remove(dayNum)
                } else {
                    if (selectedPositions.size < MAX_SELECT_COUNT) {
                        selectedPositions.add(dayNum)
                    }else{
                        "最多只能选择3个提醒".toToast()
                    }
                }
                notifyItemChanged(pos)
            }
        }
    }

    override fun onBindViewHolder(holder: ItemRemindViewHolder, position: Int) {
        val item = data.getOrNull(position)
        holder.bind(item, position, initSelectTimeStr)
    }

    //提供一个外部方法，获取当前所有选中的items
    fun getSelectedReminds(): MutableSet<Int> {
        return selectedPositions
    }

    class ItemRemindViewHolder(
        private val binding: ItemRemindBinding,
        private val selectedPositions: MutableSet<Int>,
        private val onItemClick: (Int) -> Unit,
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: RemindBean?, position: Int, selectTime: String) {
            item?.let {
                val selected = selectedPositions.contains(it.dayNum)
                if(item.dayNum in 0 ..99){
                    binding.tvDesc.text = it.content + "(${selectTime})"
                }else{
                    binding.tvDesc.text = it.content
                }
                binding.ivSelected.loadImage(if (selected) R.drawable.icon_selected else R.drawable.icon_unselected)
            }

            //设置item点击事件
            binding.root.setSingleClick {
                onItemClick(position)
            }
        }
    }

    private fun showAdvancePop() {
        DialogUtils.showBasePop(context, outDismiss = true, showBg = true)
            .asCustom(AdvanceRemindDaysPop(context){day ->
                if(!selectedPositions.contains(day)){
                    selectedPositions.add(day)
                }
                if(!data.any { it.dayNum == day }){
                    data.add(RemindBean(day, "提前${day}天"))
                }
                data.sortBy { it.dayNum }
                notifyDataSetChanged()
            }).show()
    }

}